package com.rdc.dictionary.daily;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.media.MediaParser;
import android.media.MediaPlayer;
import android.net.Uri;
import android.net.UrlQuerySanitizer;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.rdc.dictionary.R;

import com.rdc.dictionary.application.App;
import com.rdc.dictionary.bean.DailySentence;
import com.rdc.dictionary.mvpBase.BaseMvpActivity;
import com.rdc.dictionary.start.MainActivity;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

/**
 * @author Soul Mate
 * @file 文件名
 * @brief 每日一句
 */

public class DailyActivity extends BaseMvpActivity<DailyPresenter, DailyActivityContract.IMainView>
		implements DailyActivityContract.IMainView
{

	private DailyPresenter mPresenter;

	private DailySentence mDailySentence = new DailySentence();

	private MediaPlayer mMediaPlayer = null;




	//图片和播放
	private ImageView mImage,mPlay;
	//中英文
	private TextView mChinese, mEnglish;

	//显示
	private RelativeLayout show;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{

		super.onCreate(savedInstanceState);

		//发送请求
		mPresenter.send();

		//监听点击播放每日一句英文
		mPlay.setOnClickListener(new View.OnClickListener()
		{
			@Override public void onClick(View v)
			{
				playMP3(mDailySentence.getMp3(),App.sContext);

			}
		});




	}

	@Override
	public void onDestroy(Bundle savedInstanceState)
	{
		super.onDestroy();

	}



	@Override public void showData(DailySentence dailySentence)
	{

		show.setVisibility(View.VISIBLE);
		mImage.setImageBitmap(dailySentence.getImage());
		mChinese.setText(dailySentence.getChinese());
		mEnglish.setText(dailySentence.getEnglish());

	}

	//view获取数据
	@Override public void get(DailySentence dailySentence)
	{
		 this.mDailySentence.setMp3(dailySentence.getMp3());
		this.mDailySentence.setImage(dailySentence.getImage());
		this.mDailySentence.setEnglish(dailySentence.getEnglish());
		this.mDailySentence.setChinese(dailySentence.getChinese());

	}

	//创建presenter
	@Override protected DailyPresenter CreatePresenter()
	{
		return new DailyPresenter();
	}

	/**
	 * 控件ID的获取以及初始化
	 * 获取presenter
	 */
	@Override protected void init()
	{
		//获取ui控件
		mImage = findViewById(R.id.iv_image);
		mPlay = findViewById(R.id.iv_play_daily);
		mChinese = findViewById(R.id.tv_chinese);
		mEnglish = findViewById(R.id.tv_english);
		show = findViewById(R.id.ll_daily);

		mPresenter = getPresenter();
		//初始化presenter
		mPresenter.init();
	}


	public void playMP3( String address, Context context) {

		//若正在播放先释放后重播
		if (mMediaPlayer != null)
		{
			if (mMediaPlayer.isPlaying())
			{
				mMediaPlayer.stop();
			}
			mMediaPlayer.release();
			mMediaPlayer = null;
		}

		//新建MediaPlayer Uri.parse有问题
//		mMediaPlayer = MediaPlayer.create(context, Uri.parse(address));
//		mMediaPlayer.start();

		mMediaPlayer = new MediaPlayer();
		Uri uri=Uri.parse(address);
		try
		{
			mMediaPlayer.setDataSource(App.sContext,uri);
			mMediaPlayer.prepare();

		} catch (IOException e)
		{
			e.printStackTrace();
		}

		mMediaPlayer.start();

	}


	@Override protected int getLayoutResId()
	{
		return R.layout.activity_daily;
	}
}
